package com.control.qct.businesssteward.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.GoodsBrandsAdapter;
import com.control.qct.businesssteward.adapter.GoodsSupplierAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.BrandModel;
import com.control.qct.businesssteward.model.SupplierModel;
import com.control.qct.businesssteward.utils.EditTextUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 获取主供应商
 * Created by Administrator on 2017/12/19 0019.
 */

public class GoodsSupplierActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        HttpUtils.IHttpResult {
    @BindView(R.id.et_search)
    EditText mEtSearch;
    @BindView(R.id.iv_delete)
    ImageView mIvDelete;
    @BindView(R.id.tv_search)
    TextView mTvSearch;
    @BindView(R.id.rv_goods_measure_unit)
    RecyclerView mRvGoodsMeasureUnit;
    private GoodsSupplierAdapter mGoodsSupplierAdapter;
    private String mSupplierId;
    private int page = 1;
    private final int row = 20;
    private List<SupplierModel.DatasBean> mSupplierModelList;//供应商列表
    private String title;

    public GoodsSupplierActivity() {
        super(R.layout.fragment_goods_measure_unit);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mSupplierId = extras.getString("supplierId");
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("主供应商");
        mSupplierModelList = new ArrayList<>();
        getSuppliers(true, "");
        mRvGoodsMeasureUnit.setLayoutManager(new LinearLayoutManager(this));
        mGoodsSupplierAdapter = new GoodsSupplierAdapter(mSupplierModelList);
        mRvGoodsMeasureUnit.setAdapter(mGoodsSupplierAdapter);
        mGoodsSupplierAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mGoodsSupplierAdapter.setPosition(position);
                Intent intent = new Intent();
                intent.putExtra("data", ((List<SupplierModel.DatasBean>) adapter.getData()).get(position).getTitle());
                intent.putExtra("supplierId", ((List<SupplierModel.DatasBean>) adapter.getData()).get(position).getId
                        ());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        EditTextUtils.isEditContent(mEtSearch, mIvDelete);
    }


    @OnClick({R.id.iv_delete, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_delete:
                mEtSearch.setText("");
                break;
            case R.id.tv_search:
                page = 1;
                mSupplierModelList.clear();
                title = mEtSearch.getText().toString();
                getSuppliers(true, title);
                break;
        }
    }

    /**
     * 获取主供应商
     */
    private void getSuppliers(boolean isShow, String title) {
        mNetProtocol.getSuppliers(this, isShow, title, page, row, this);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        getSuppliers(false, title);
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        mEtSearch.setText("");
        SupplierModel supplierModel = (SupplierModel) baseBean.getData();
        mSupplierModelList.addAll(supplierModel.getDatas());
        if (mGoodsSupplierAdapter != null) {
            if (supplierModel.getDatas().size() > 9) {
                mGoodsSupplierAdapter.setOnLoadMoreListener(this, mRvGoodsMeasureUnit);
                mGoodsSupplierAdapter.loadMoreComplete();
            } else {
                mGoodsSupplierAdapter.loadMoreEnd();
            }
            mGoodsSupplierAdapter.notifyDataSetChanged();
            for (int i = 0; i < mSupplierModelList.size(); i++) {
                if (mSupplierModelList.get(i).getId().equals(mSupplierId)) {
                    mGoodsSupplierAdapter.setPosition(i);
//                    mRvGoodsMeasureUnit.smoothScrollToPosition(i);
                }
            }
        }
    }

    @Override
    public void onFail(BaseBean baseBean) {

    }
}
